package com.hot100.mid;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 搜索旋转排序数组,其实就是二分查找，稍微变了下形
 * @date 2022/8/6 16:13
 */
public class Demo11 {
    public static void main(String[] args) {
        int[] nums={1,3};
        int search = search(nums, 3);
        System.out.println(search);

    }
    public static int search(int[] nums, int target) {

        if(nums.length==1&&nums[0]==target){
            return 0;
        }
        int left=0,right=nums.length-1;
        while (left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target){
                return mid;
            }
            //左边有序
            if(nums[left]<=nums[mid]){
                //正好在左边有序的里面，则right=mid-1.否则left=mid
                if(target>=nums[left]&&target<nums[mid]){
                    right=mid;
                }else {
                    left=mid+1;
                }
            }
            //右边有序
            else {
                //正好在右边里面，这left=mid 否则
                if(target>nums[mid]&&target<=nums[right]){
                    left=mid+1;
                }else {
                    right=mid;
                }
            }
        }
      return -1;
    }
}
